﻿using Microsoft.SqlServer.Server;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Guoc.BigMall.Infrastructure.Payment.AliPay
{
   public class AliPayRequest
    {
        SortedDictionary<string, string> _content = new SortedDictionary<string, string>();

        /// <summary>
        ///  跟路径
        /// </summary>
        public string webRootPath { get; set; }
        /// <summary>
        ///  uft-8
        /// </summary>
        public string Charset { get { return "utf-8"; } }

        public void Add(string key, string value)
        {
            if (!_content.ContainsKey(key))
            {
                _content.Add(key, value);
            }
        }

        public string GetValue(string key)
        {
            if (_content.ContainsKey(key))
            {
                return _content[key];
            }
            else
            {
                return "";
            }
        }

        /// <summary>
        ///  数据内容
        /// </summary>
        public SortedDictionary<string, string> Content { get { return _content; } }

        /// <summary>
        ///  获取签名内容
        /// </summary>
        /// <returns></returns>
        public string GetSignContent()
        {
            // 第一步：把字典按Key的字母顺序排序
            // IDictionary<string, string> sortedParams = new SortedDictionary<string, string>(parameters);
            IDictionary<string, string> sortedParams = _content;
             IEnumerator <KeyValuePair<string, string>> dem = sortedParams.GetEnumerator();

            // 第二步：把所有参数名和参数值串在一起
            StringBuilder query = new StringBuilder("");
            while (dem.MoveNext())
            {
                string key = dem.Current.Key;
                string value = dem.Current.Value;
                if (!string.IsNullOrEmpty(key) && !string.IsNullOrEmpty(value))
                {
                    query.Append(key).Append("=").Append(value).Append("&");
                }
            }
            string content = query.ToString().Substring(0, query.Length - 1);

            return content;
        }

        public string SerializeObject()
        {
            return JsonConvert.SerializeObject(this._content, Formatting.Indented, new JsonSerializerSettings
            {
                ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
                DateFormatString = "yyyy-MM-dd HH:mm:ss", //输出的日期格式
               // ContractResolver = new CamelCasePropertyNamesContractResolver(), //设置属性的名称为“驼峰命名法”
                NullValueHandling = NullValueHandling.Ignore
            });
        }

        public SortedDictionary<string, string> DeserializeObject(string data)
        {
            var content=  JsonConvert.DeserializeObject<SortedDictionary<string,string>>(data, new JsonSerializerSettings
            {
                ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
                DateFormatString = "yyyy-MM-dd HH:mm:ss", //输出的日期格式
               // ContractResolver = new CamelCasePropertyNamesContractResolver(), //设置属性的名称为“驼峰命名法”
                NullValueHandling = NullValueHandling.Ignore
            });
            this._content.Clear();
            this._content = content;
            return content;
        }
    }
}
